package com.totoro.dubbo.consumer.controller;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.annotation.Reference;
import com.totoro.dubbo.provider.service.ICookService;
import com.totoro.dubbo.provider.service.IHelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;

/**
 * Created by Niki on 2018/7/2 9:02
 */
@Controller
public class HelloController {
    IHelloService helloService;

    @Reference
    ICookService cookService;

    @Autowired
    ApplicationConfig applicationConfig;
    @Autowired
    RegistryConfig registryConfig;


    @GetMapping("/hello")
    @ResponseBody
    public String hello() {
        String hello = helloService.sayHello();
        return hello;
    }

    @GetMapping
    @ResponseBody
    public String cook() {
        String cook = cookService.cook();
        return cook;
    }

    @PostConstruct
    public void reference() {
        ReferenceConfig<IHelloService> helloServiceReference = new ReferenceConfig<>();
        helloServiceReference.setApplication(this.applicationConfig);
        helloServiceReference.setRegistry(this.registryConfig);

        helloServiceReference.setInterface(IHelloService.class);
        helloServiceReference.setVersion("1.0.0");

        helloService = helloServiceReference.get();
    }
}
